#include "dialog.h"
#include "ui_dialog.h"

//-------------------构造函数C----------------
Dialog::Dialog(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::Dialog)
{
    ui->setupUi(this);
    this->setWindowOpacity(0.9);
    connect2DB();
}

//---------------------析构函数D---------------
Dialog::~Dialog()
{
    if(db.isOpen())
        db.close();
    delete ui;
}

void Dialog::createTable()
{
    QString sql = "CREATE TABLE student(id INTEGER PRIMARY KEY,name TEXT NOT NULL,subject TEXT,score FLOAT);";
    QSqlQuery  sq;
    if(sq.exec(sql))
    {
        qDebug() << "建表成功！";

    }else
    {
        QString text = sq.lastError().text();
        qDebug() << "create table err:" << text;
    }
}

void Dialog::insertRow()
{
    //获得用户输入
}



void Dialog::on_pushButton_7_clicked()
{
    close();
}

void Dialog::on_pushButton_add_clicked()
{
    ui->pushButton_add->setEnabled(false);
    add_Student *as = new add_Student();

    //add_student页面关闭信号捕捉
    connect(as,SIGNAL(add_close()),this,SLOT(add_close_slot()));

    as->show();
}
//--------------------关闭捕捉槽函数
void Dialog::add_close_slot()
{
    ui->pushButton_add->setEnabled(true);
}

void Dialog::paintEvent(QPaintEvent *event)
{
    int w = width();
    int h = height();
    QPainter painter(this);
    QPixmap map(":/new/back/VCG41N670692848.jpg");
    painter.drawPixmap(0,0,w,h,map);

}

//------------------------数据库连接函数
void Dialog::connect2DB()
{
    //通过不同数据库的驱动程序连接不同的数据库
    db = QSqlDatabase::addDatabase("QSQLITE");     //添加SQLite3
    //设置SQLite数据库文件的名称
    db.setDatabaseName("student_info.db");
    //设置连接的用户名密码
    db.setUserName("admin");
    db.setPassword("admin");
    //打开数据库链接
    if(db.open())
    {
        qDebug() << "数据库连接成功!";
        createTable();
    }
    else
    {
        QSqlError error = db.lastError();
        //获得错误信息
        QString text = error.text();
        //展示错误信息
        QMessageBox::critical(this,"错误",text);
    }

}
//判断人是否存在
bool Dialog::isExist(QString i)
{
    QString sql = "SELECT * FROM student WHERE id=?";
    QSqlQuery sq;
    sq.prepare(sql);
    sq.addBindValue(i);
    if(sq.exec())
    {
        if(sq.next())
        {
            return true;
        }
    }else
    {
        qDebug() << "查无此人" ;
        return false;
    }
    return false;
}
//删除槽函数
void Dialog::on_pushButton_delete_clicked()
{
    if(ui->lineEdit_2->text() == "")
    {
        QMessageBox::warning(this,"警告","请输入删除人的学号!");
        return;
    }
    if(isExist(ui->lineEdit_2->text()))
    {
    QSqlQuery sq;
    QString sql = "DELETE FROM student WHERE id = ?";
    //先对SQL语句进行预处理
    sq.prepare(sql);
    sq.addBindValue(ui->lineEdit_2->text());
    if(sq.exec())
    {
        QMessageBox::information(this,"通知","删除成功!");
    }
    else
    {
        QString text = sq.lastError().text();
        qDebug() << text;
        QMessageBox::warning(this,"警告","删除失败！");
    }
    }else
    {
        QMessageBox::warning(this,"提示","查无此人");
        return;
    }
    return;
}

void Dialog::on_pushButton_modify_clicked()
{
    if(ui->lineEdit_2->text() == "")
    {
        QMessageBox::warning(this,"警告","请输入修改人的学号!");
        return;
    }
    if(isExist(ui->lineEdit_2->text()))
    {
        Modify *mf = new Modify(ui->lineEdit_2->text(),this);
        mf->show();
    }else
    {
        QMessageBox::warning(this,"提示","查无此人");
        return;
    }
}

void Dialog::on_pushButton_searchall_clicked()
{
    ui->textBrowser_2->clear();
       QString sql = "SELECT * FROM student";
       QSqlQuery sq;
       if(sq.exec(sql))
       {

           while(sq.next())
           {

               QString id = sq.value(0).toString();
               QString name = sq.value("name").toString();
               QString subject = sq.value("subject").toString();
               QString score = sq.value("score").toString();
               id.append("-").append(name).append("-").append(subject).append("-").append(score);

               ui->textBrowser_2->append(id);
           }
       }else
       {
           QString text = sq.lastError().text();
           QMessageBox::critical(this,"查询失败:",text);
       }
}

void Dialog::on_pushButton_open_clicked()
{
    srcPath = QFileDialog::getOpenFileName(this,"打开","D:/",
                                    "Sql(*.db)");
       if(srcPath == "")
       {
           ui->textBrowser->clear();
           ui->textBrowser->append("您未选取文件！");
           return;
       }
}
//关键字模糊查询
void Dialog::on_pushButton_8_clicked()
{
    QString name = ui->lineEdit_2->text();
    if(name == "")
    {
         QMessageBox::warning(this,"提示","请输入姓名");
         return;
    }
    QString sql = "SELECT * FROM student WHERE name LIKE ?";
    QSqlQuery sq;
    sq.prepare(sql);
    sq.addBindValue(name.prepend("%").append("%"));
    if(sq.exec())
    {
        ui->textBrowser_2->clear();
        if(sq.next())
        {
            do
            {
                QString id = sq.value(0).toString();
                QString name = sq.value("name").toString();
                QString subject = sq.value("subject").toString();
                QString score = sq.value("score").toString();
                id.append("-").append(name).append("-").append(subject).append("-").append(score);
                ui->textBrowser_2->append(id);
            }while(sq.next());
        }
        else
        {
            QMessageBox::warning(this,"提示","没有查询结果!");
        }
    }else
    {
        QString text = sq.lastError().text();
        QMessageBox::critical(this,"查询失败:",text);
    }

}
